/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.pacs.actor;

import akka.actor.ActorRef;
import akka.actor.UntypedActor;
import com.cxhd.pacs.tools.DicomImage;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.awt.image.BufferedImage;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Map;
import javax.imageio.ImageIO;
import static javax.print.attribute.ResolutionSyntax.DPI;
import org.jpedal.PdfDecoder;
import org.jpedal.exception.PdfException;

/**
 *
 * @author Administrator
 */
public class PdfToJpgActor extends UntypedActor {

    DicomImage di = new DicomImage();

    @Override
    public void onReceive(Object o) throws Exception {
        String path = (String) o;
        runJpg2(path);

    }

    public void runToJpg(String path) {

        try {
            Runtime r = Runtime.getRuntime();

            Process p = r.exec("cmd.exe /c c:/dcmsyst/tools/pdfbox-app-2.0.4.jar  ExtractImages " + path);
            // System.out.println("cmd.exe /c c:/dcmsyst/tools/dcm2jpg.exe -s y -q 70 -o " + baseDir + "/image/ " + fs.getAbsolutePath());
            p.waitFor();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
//        try {
//            di.saveImage(info.get("dicom_path"), targerImagePath);
//        } catch (Exception ex) {
//            ex.printStackTrace();
//        }
    }

    public void runJpg2(String path) {
        File f = new File(path);
        if (f.exists()) {
            PdfDecoder decode_pdf = new PdfDecoder(true);

            try {

                decode_pdf.setExtractionMode(PdfDecoder.TEXT + PdfDecoder.RAWIMAGES
                        + PdfDecoder.FINALIMAGES, DPI, DPI / 36);
                decode_pdf.openPdfFile(path); //file

                int start = 1, end = decode_pdf.getPageCount();

                for (int i = start; i < end + 1; i++) {

                    BufferedImage img = decode_pdf.getPageAsImage(i);

                    try {

                        ImageIO.write(img, "jpg", new File(f.getParent()+"/" + i + ".jpg"));

                    } catch (IOException e) {

                        e.printStackTrace();

                    }

                }

                decode_pdf.closePdfFile();

            } catch (PdfException e) {

                e.printStackTrace();

            }
        }
    }

}
